package com.beiding.t;

import java.util.UUID;

/**
 * @author 丁常磊
 * @date 2022/2/11 14:06
 */

public class JDBC声明式SQL转普通SQL {

    public static void main(String[] args) {

        String a = h("select * from ( select a.src_orderno as src_orderno,a.ent_id as ent_id, a.group_id as group_id, g.name as group_name, a.type_id as type_id, t.name as type_name, a.cid as cid, a.media as media, a.accnt_no as accnt_no, a.face_value as face_value, a.use_scope as use_scope, a.src_buid as src_buid, a.src_channel as src_channel, a.getevtid as getevtid, sum(a.balance) as balance, a.status as status, (Case when a.group_id='05' then 0 else 1 end) as roe, g.needpwd as needpwd, t.paycode as paycode, g.allow_outpay as outpay, ifnull(t.Cond_Amt, 0) As Cond_Amt, t.description as coupon_desc, (Case when t.group_id='01' then 'H' else null end) as point_usemode, (Case when t.usertype='2' then 'Y' else 'N' end) as is_money_coupon, a.memo, t.is_jf, t.is_pop, t.usertype, (Case when a.accnt_no='0' then 0 else batch_id end) as batch_id,a.src_paycode as src_paycode ,a.eff_date as eff_date, a.exp_date as exp_date, sum(ifnull(a.cash_cost, 0)) as cash_cost, sum(ifnull(a.cost_point, 0)) as point_cost ,(case a.group_id when '05' then 0 else fgetaccntroe(?,?,?,type_id,current_date()) end) as poproe from accnt_batch a, accnt_group g, accnt_type t query a.group_id = g.gid and a.type_id = t.tid and a.group_id = t.group_id and a.ent_id = g.ent_id and a.ent_id = t.ent_id and a.ent_id = ? and a.cid = ? and a.status = 'Y' and a.balance <> 0 and a.eff_date <= current_date() and a.exp_date >= current_date() and a.group_id in ('01') and (a.use_scope in ('1', '0') or (a.use_scope = '2' and a.src_buid = ?) or (use_scope = '3' and a.src_channel = ?) or (a.use_scope = '4' and a.src_buid = ? and a.src_channel = ?)) group by a.ent_id , a.group_id, g.name , a.type_id , t.name , a.cid , a.media , a.accnt_no , a.face_value, a.use_scope , a.src_buid , a.src_channel , a.getevtid , a.status , g.needpwd , t.paycode, g.allow_outpay , t.cond_amt , t.description , t.usertype, a.memo, t.is_jf, t.is_pop,a.batch_id ,a.eff_date , a.exp_date ) a order by exp_date, use_scope desc", "0(Long), V3(String), 6040(String), 0(Long), 9800258149022(String), 6040(String), V3(String), 6040(String), V3(String)");

        System.out.println(a);

    }

    public static String h(String sql, String pms) {

        String[] pm = pms.split(",");

        StringBuilder builder = new StringBuilder();

        String[] split = sql.split("\\?");

        for (int i = 0; i < split.length - 1; i++) {
            String tx = split[i];
            builder.append(tx);
            String p = pm[i];
            p = p.trim();
            int index = p.lastIndexOf("(");
            String v = p.substring(0, index);
            String tp = p.substring(index);
            if ("(String)".equals(tp)) {
                builder.append("'").append(v).append("'");
            } else {
                builder.append(v);
            }

        }

        builder.append(split[split.length - 1]);

        return builder.toString();
    }

}
